﻿// ----------------------------------------------------------------------
// <copyright file="PathSiteInfoGetter.cs" company="Meligo">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------

namespace Meligo.ModuleExtractor.GenerateModuleFactory.Helper
{
    using System.Collections.Generic;
    using Meligo.ModuleExtractor.GenerateModuleFactory.Entities;
    using Microsoft.SharePoint;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class PathSiteInfoGetter
    {
        public static PathSiteInfo GetPathSiteInfo(string host, string path, Dictionary<string, PathSiteInfo> pathSiteInfoMap)
        {
            string text = path;
            if (text.EndsWith("/"))
            {
                text = text.TrimEnd(new char[] { '/' });
            }
            else
            {
                string[] array = text.Split(new char[] { '/' });
                string text2 = array[array.Length - 1];
                if (text2.IndexOf('.') > 0)
                {
                    text = string.Join("/", array, 0, array.Length - 1);
                }
            }

            PathSiteInfo result;
            if (pathSiteInfoMap.ContainsKey(text))
            {
                result = pathSiteInfoMap[text];
            }
            else
            {
                PathSiteInfo pathSiteInfo = null;
                string text3 = host.TrimEnd(new char[] { '/' }) + "/" + text.TrimStart(new char[] { '/' });
                try
                {
                    SPSite sharepointSite = new SPSite(text3);
                    //{
                    SPWeb sharepointWeb = sharepointSite.OpenWeb();
                    //{
                    if (sharepointWeb.Exists)
                    {
                        pathSiteInfo = new PathSiteInfo
                        {
                            SiteID = sharepointSite.ID,
                            WebID = sharepointWeb.ID
                        };
                    }
                    //}
                    //}
                }
                catch
                {
                    // throw 
                }

                if (pathSiteInfo == null)
                {
                    pathSiteInfo = new PathSiteInfo();
                }

                pathSiteInfoMap[text] = pathSiteInfo;
                result = pathSiteInfo;
            }

            return result;
        }
    }
}
